#!/usr/bin/php -q
<?PHP
/* Copyright 2005-2023, Lime Technology
 * Copyright 2012-2023, Bergware International.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License version 2,
 * as published by the Free Software Foundation.
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 */
?>
<?
$docroot ??= ($_SERVER['DOCUMENT_ROOT'] ?: '/usr/local/emhttp');
require_once "$docroot/plugins/dynamix.plugin.manager/include/PluginHelpers.php";

function readJson($file) {
  return is_file($file) ? json_decode(file_get_contents($file),true) : [];
}
function newurl($url) {
  $oldURL = 'https://raw.github.com/';
  $newURL = 'https://raw.githubusercontent.com/';
  return str_replace($oldURL,$newURL,$url);
}
function searchLink(&$db,$url) {
  if ($url) for ($i = 0; $i < count($db); $i++) if ( ($db[$i]['PluginURL']??null)==$url) return $db[$i]['Support']??null;
}

$type    = $argv[1]??'';   // plugin or language
$method  = $argv[2]??'';   // install, update, remove, check
$name    = $argv[3]??'';   // plugin name (*.plg) or language name (*.xml)
$error   = $argv[4]??'';   // error code (empty if none)

$plugin  = "/boot/config/plugins/$name";
$pending = "/tmp/plugins/pluginPending";

switch ($type) {
case 'plugin':
  switch ($method) {
  case 'install':
  case 'update':
    // abort if method was unsuccessful
    if ($error) break;
    // update support link in plugin file
    $info = readJson('/tmp/community.applications/tempFiles/templates.json');
    // find matching support link
    $url = plugin('pluginURL', $plugin);
    if ($support = searchLink($info, $url) ?: searchLink($info, newurl($url))) {
      // update incorrect or missing support links
      if (plugin('support', $plugin) != $support) {
        $xml = @simplexml_load_file($plugin);
        if ($xml->xpath('//PLUGIN/@support')[0]??false) {
          // support link exists, update it
          $xml->xpath('//PLUGIN/@support')[0] = $support;
        } else {
          // support link is missing, add it
          $xml->addAttribute('support', $support);
        }
        echo "Updating support link\n";
        file_put_contents($plugin, $xml->saveXML());
      }
    }
  }
  break;
case 'language':
  // nothing defined
  break;
}

// unset pending status
if ($method != 'check') @unlink("$pending/$name");
?>
